package com.by.web;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.by.aop.Operation;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@SuppressWarnings({"all"})
public class LoginController {
	
	private Logger log = LoggerFactory.getLogger(LoginController.class);

	private static final String CMN_PREFIX = "/cmn";

   // @RequestMapping(value = "/{source}/login", method = RequestMethod.GET)
   // public String accessLoginPageGet(@PathVariable("source") String source, HttpServletRequest request, Model model) throws Exception {
     @RequestMapping(value = "/login", method = RequestMethod.GET)
     public String accessLoginPageGet(HttpServletRequest request, Model model) throws Exception {
	     Map<String, ?> map = RequestContextUtils.getInputFlashMap(request);

	     return CMN_PREFIX + "/login";
    }

    @GetMapping("/error/{errorcode}")
    public String error401(@PathVariable("errorcode") String errorcode,HttpServletRequest request, HttpServletResponse response) {

        return "/"+errorcode;
    }


    @GetMapping("/code/image")
    @Operation(value = "用户测试")
    public void ddd(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        request.getSession().setAttribute("ShearCaptcha", captcha);
        captcha.write(response.getOutputStream());
    }

//    @RequestMapping(value = "/{source}/login", method = RequestMethod.POST)
//    public String accessLoginPagePost(@PathVariable("source") String source, HttpServletRequest request, Model model,
//                                      RedirectAttributes redirectAttributes) throws Exception {
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String accessLoginPagePost(HttpServletRequest request, Model model,
                                      RedirectAttributes redirectAttributes) throws Exception {
        String exceptionMessage = (String) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
        if(!StringUtils.isEmpty(exceptionMessage)) {
            redirectAttributes.addFlashAttribute("error", exceptionMessage);
            redirectAttributes.addFlashAttribute("username", ServletRequestUtils.getStringParameter(request, "username", ""));
            log.error("登录出错。。。");
        }

        return "redirect:/login";
    }

}
